//
//  DLGroupListView.m
//  DontRem
//
//  Created by apple on 2021/1/22.
//

#import "DLGroupListView.h"
#import "DLGroupModel.h"
#import "DLGroupListCell.h"
@interface DLGroupListView()<UITableViewDelegate,UITableViewDataSource,UIGestureRecognizerDelegate>
@property(nonatomic,retain)UITableView * myTab;
@property(nonatomic,retain)NSMutableArray * dataArr;
@end
@implementation DLGroupListView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.7];
        UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dismiss)];
        tap.delegate = self;
        [self addGestureRecognizer:tap];
        
        self.dataArr = [NSMutableArray new];
        self.myTab = [[UITableView alloc]initWithFrame:Frame(0, SCREEN_HEIGHT, SCREEN_WIDTH, 0.6 * SCREEN_HEIGHT - KTabbarSafeBottomMargin) style:UITableViewStyleGrouped];
        self.myTab.delegate = self;
        self.myTab.dataSource = self;
        self.myTab.backgroundColor = [UIColor normalColor:UnderLineColor darkColor:UIColorBlack];
        self.myTab.separatorStyle = UITableViewCellSeparatorStyleNone;
        self.myTab.rowHeight = 55;
        [self addSubview:self.myTab];
        [self.myTab registerNib:[UINib nibWithNibName:@"DLGroupListCell" bundle:nil] forCellReuseIdentifier:@"DLGroupListCell"];
        [UIView animateWithDuration:0.35 animations:^{
            self.myTab.y = 0.4 * SCREEN_HEIGHT + KTabbarSafeBottomMargin;            
        }];        
    }
    return self;
}
-(void)start{
    [self getData];
}
- (void)setUnshowId:(NSNumber *)unshowId{
    _unshowId = unshowId;
}
-(void)getData{
    NSArray* finfAlls = [DLGroupModel bg_findAll:DLGroupTab];
    if (self.unshowId) {
        for (DLGroupModel * mo  in finfAlls) {
            if (![mo.bg_id isEqual:self.unshowId]) {
                [self.dataArr addObject:mo];
            }
        }
    }else{
        [self.dataArr addObjectsFromArray:finfAlls];
    }    
    [self.myTab reloadData];
}
-(void)addGroupClick{
    QMUIDialogTextFieldViewController *dialogViewController = [[QMUIDialogTextFieldViewController alloc] init];
    dialogViewController.title = @"温馨提示";
    [dialogViewController addTextFieldWithTitle:@"分类名称" configurationHandler:^(QMUILabel *titleLabel, QMUITextField *textField, CALayer *separatorLayer) {
        textField.placeholder = @"请输入分类名称";
        textField.maximumTextLength = 10;
    }];
    [dialogViewController addCancelButtonWithText:@"取消" block:nil];
    [dialogViewController addSubmitButtonWithText:@"确定" block:^(QMUIDialogTextFieldViewController *aDialogViewController) {
        UITextField * tf = aDialogViewController.textFields.firstObject;
        if (tf.text.length > 0) {
            if ([tf.text isEqualToString:@"默认分组"]) {
                XHQHUDErrorText(@"不能与系统关键词重名，请重新选择");
            }else{
                [aDialogViewController hide];
                DLGroupModel * mo = [DLGroupModel new];
                mo.groupName = tf.text;
                mo.bg_tableName = DLGroupTab;
                [mo bg_save];
                [self.dataArr removeAllObjects];
                [self getData];
            }
        } else {
            XHQHUDErrorText(@"分类名称不能为空");
        }
    }];
    [dialogViewController show];
}
#pragma mark 代理方法
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    DLGroupListCell * cell = [tableView dequeueReusableCellWithIdentifier:@"DLGroupListCell"];
    if (indexPath.row < self.dataArr.count) {
        DLGroupModel * mo = self.dataArr[indexPath.row];
        cell.groupName = mo.groupName;
    }
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 5;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 60;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return nil;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView * footer = [UIView new];
    footer.backgroundColor = UnderLineColor;
    CGFloat w = SCREEN_WIDTH - 20;
    QMUIButton * btn = [QMUIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = Frame(10, 0, w, 55);
    [btn setTitle:@"添加分组" forState:UIControlStateNormal];
    [btn setTitleColor:TextSubColor forState:UIControlStateNormal];
    btn.titleLabel.font = UIFontMake(floor(AUTO(18)));
    btn.spacingBetweenImageAndTitle = 10;
    [btn setImagePosition:QMUIButtonImagePositionLeft];
    [btn setImage:Image(@"添加_方框") forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(addGroupClick) forControlEvents:UIControlEventTouchUpInside];
    [footer addSubview:btn];
    CAShapeLayer *layer   = [[CAShapeLayer alloc] init];
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:Frame(0, 0, w, 55) cornerRadius:4.0f];
    layer.path = path.CGPath;
    layer.lineWidth = 1.0f;
    layer.lineDashPattern = @[@4, @4];
    layer.fillColor = [UIColor clearColor].CGColor;
    layer.strokeColor = [UIColor grayColor].CGColor;
    [btn.layer addSublayer:layer];
    return footer;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row < self.dataArr.count) {
        DLGroupModel * mo = self.dataArr[indexPath.row];
        [self dismissWithModel:mo];
    }
}
-(void)dismiss{
    [self dismissWithModel:nil];
}
-(void)dismissWithModel:(DLGroupModel *)mo{
    [UIView animateWithDuration:0.35 animations:^{
        self.myTab.y = SCREEN_HEIGHT;
    } completion:^(BOOL finished) {
        if (self.dismissEndBlock) {
            self.dismissEndBlock(mo);
        }
    }];    
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    if ([NSStringFromClass([touch.view.superview class]) isEqualToString:@"UITableViewCellContentView"]) {
        return NO;
    }else {
        return YES;
    }
}
@end
